package com.lxp.project.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.lxp.common.constant.HttpStatus;
import com.lxp.common.utils.SecurityUtils;
import com.lxp.common.utils.poi.ExcelUtil;
import com.lxp.framework.web.domain.AjaxResult;
import com.lxp.framework.web.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.lxp.project.function.mapper.FnOrderMapper;
import com.lxp.project.function.domain.FnOrder;
import com.lxp.project.function.service.IFnOrderService;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约管理Service业务层处理
 *
 * @author 乐乐
 * @date 2022-05-04
 */
@Service
public class FnOrderServiceImpl extends ServiceImpl<FnOrderMapper, FnOrder> implements IFnOrderService {
    @Resource
    private FnOrderMapper fnOrderMapper;

    @Override
    public List<FnOrder> getAllList() {
        return fnOrderMapper.selectList(null);
    }

    @Override
    public TableDataInfo getPageList(FnOrder fnOrder, TableDataInfo tableSupport) {
        LambdaQueryWrapper<FnOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (fnOrder.getProjectId() != null) {
            queryWrapper.eq(FnOrder::getProjectId, fnOrder.getProjectId());
        }
        if (!SecurityUtils.isAdmin()) {
            queryWrapper.eq(FnOrder::getUserId, SecurityUtils.getUserId());
        }
        Integer count = fnOrderMapper.selectCount(queryWrapper);
        IPage<FnOrder> iPage = fnOrderMapper.selectPage(new Page<>(tableSupport.getPageNum(), tableSupport.getPageSize()), queryWrapper);
        tableSupport.setCode(HttpStatus.SUCCESS);
        tableSupport.setMsg("查询成功");
        tableSupport.setRows(iPage.getRecords());
        tableSupport.setTotal(count);
        return tableSupport;
    }

    @Override
    public AjaxResult saveData(FnOrder fnOrder) {
        fnOrder.setUserId(SecurityUtils.getUserId());
        return AjaxResult.msg(fnOrderMapper.insert(fnOrder));
    }

    @Override
    public AjaxResult updateDataById(FnOrder fnOrder) {
        return AjaxResult.msg(fnOrderMapper.updateById(fnOrder));
    }

    @Override
    public AjaxResult removeByIds(List<Long> ids) {
        return AjaxResult.msg(fnOrderMapper.deleteBatchIds(ids));
    }

    @Override
    public AjaxResult exports(FnOrder fnOrder) {
        LambdaQueryWrapper<FnOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (fnOrder.getProjectId() != null) {
            queryWrapper.eq(FnOrder::getProjectId, fnOrder.getProjectId());
        }
        if (fnOrder.getUserId() != null) {
            queryWrapper.eq(FnOrder::getUserId, fnOrder.getUserId());
        }
        List<FnOrder> list = fnOrderMapper.selectList(queryWrapper);
        ExcelUtil<FnOrder> util = new ExcelUtil<>(FnOrder.class);
        return util.exportExcel(list, "fnOrder");
    }

    @Override
    public FnOrder getById(Long id) {
        return fnOrderMapper.selectById(id);
    }
}
